package main

import (
	"strconv"
	"strings"
)

func myAtoi(s string) int {
	s = strings.TrimSpace(s)
	runes := []rune(s)
	var sign rune // 负号
	var nums []rune // 数字部分
	if len(s) != 0 {
		if runes[0] == '+' {
			runes = runes[1:] // 舍弃正号不参与运算
		} else if runes[0] == '-' {
			sign = runes[0] // 暂存负号
			runes = runes[1:] // 舍弃负号不参与运算
		}
	}
	for _,char := range runes {
		if len(nums) == 0 && char == '0' {
			continue
		}
		if char < 48 || char > 57 {
			break
		}
		nums = append(nums, char)
	}
	s = string(nums)
	if sign != 0 {
		s = string(sign) + s // 补上负号
	}
	i32, _ := strconv.Atoi(s)
	if i32 < -2147483648 {
		i32 = -2147483648
	} else if i32 > 2147483647 {
		i32 = 2147483647
	}
	return i32
}